package uk.ac.lkl.common.ui;



import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;


public class SwingUtilities2 {

    public static ArrayList<Component> getMouseComponents(Component component) {
        ArrayList<Component> components = new ArrayList<Component>();
        getMouseComponents(component, components);
        return components;
    }


    private static void getMouseComponents(Component component,
                                           ArrayList<Component> list) {
        MouseListener [] mouseListeners = component.getMouseListeners();
        MouseMotionListener [] mouseMotionListeners = component.getMouseMotionListeners();

        if (mouseListeners.length > 0 || mouseMotionListeners.length > 0)
            list.add(component);

        if (!(component instanceof Container))
            return;

        Container container = (Container) component;
        Component [] components = container.getComponents();
        for (int i = 0; i < components.length; i++)
            getMouseComponents(components[i], list);
    }

}
